package system

import (
	"server/global"
	sysModel "server/model/system"
	sysRequest "server/model/system/request"
)

const (
	TypeImage string = "image"
	TypeVideo string = "video"
	TypeVoice string = "voice"
	TypeFile  string = "file"
)

var FileTypeList = map[string]string{
	TypeImage: "图片",
	TypeVideo: "视频",
	TypeVoice: "音频",
	TypeFile:  "文件",
}

var FileServiceApp = new(FileService)

type FileService struct {
}

//获取列表
func (d *FileService) GetList(p *sysRequest.SysFileGetList) ([]*sysModel.SysFile, int64, error) {
	var list []*sysModel.SysFile
	var total int64
	tx := global.GvaDb.Model(&sysModel.SysFile{})
	if p.Keyword != "" {
		tx = tx.Where("origin_name like ?", "%"+p.Keyword+"%")
	}
	if p.FileType != "" {
		tx = tx.Where("file_type = ?", p.FileType)
	}
	if len(p.TimeArea) > 0 {
		//startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", searchParams.TimeArea[0], time.Local)
		//endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", searchParams.TimeArea[len(searchParams.TimeArea)-1], time.Local)
		tx = tx.Where("created_at BETWEEN ? AND ?", p.TimeArea[0], p.TimeArea[len(p.TimeArea)-1])
	}
	err := tx.Count(&total).Order("id DESC").Limit(int(p.PageSize)).Offset(int((p.PageNo - 1) * p.PageSize)).Find(&list).Error
	//for _, file := range list {
	//	file.CreatedAt = file.CreatedAt.Format("2006-01-02 15:04:05")
	//}
	return list, total, err
}
